package n叉树的遍历;

import java.util.*;

/**
 * @author: zhengtao
 * @description: N叉树的前序遍历
 * @version: 1.0
 * @date: 2022/10/29  16:08
 */
public class Demo589 {

// Definition for a Node.
class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
};
    public List<Integer> preorder(Node root) {
        List<Integer> res=new ArrayList<>();
        if(root==null){
            return res;
        }
        Stack<Node> stack=new Stack<>();
        stack.add(root);
        while (!stack.isEmpty()){
            Node pop = stack.pop();
            if(pop!=null){
                res.add(pop.val);
                List<Node> children = pop.children;
                for (int i = children.size() - 1; i >= 0; i--) {
                    stack.push(children.get(i));
                }
            }

        }
        return res;
    }
}
